//
//  UILabel+Custom.m
//  MortgageHelper
//
//  Created by zhangxiang on 16/11/3.
//  Copyright © 2016年 zhangxiang. All rights reserved.
//

#import "UILabel+Custom.h"
#import "NSString+IMTExtension.h"
#import <objc/runtime.h>
#import "UIColor+Customer.h"
@implementation UILabel (Custom)
-(UIColor *)forcedTintColor{
    return objc_getAssociatedObject(self, _cmd);
}

-(void)setForcedTintColor:(UIColor *)forcedTintColor{
    objc_setAssociatedObject(self, @selector(forcedTintColor), forcedTintColor, OBJC_ASSOCIATION_RETAIN_NONATOMIC);
}


void swizzleMethod(Class class, SEL originalSelector,SEL swizzledSelector)
{
    Method originalMethod = class_getInstanceMethod(class, originalSelector);
    Method swizzledMethod = class_getInstanceMethod(class, swizzledSelector);
    BOOL didAddMethod = class_addMethod(class, originalSelector, method_getImplementation(swizzledMethod), method_getTypeEncoding(swizzledMethod));
    if (didAddMethod)
    {
        class_replaceMethod(class, swizzledSelector, method_getImplementation(originalMethod), method_getTypeEncoding(originalMethod));
    }else{
        method_exchangeImplementations(originalMethod, swizzledMethod);
    }
}
+(void)load{
    
    swizzleMethod([self class], @selector(setTintColor:), @selector(setLabelTintColor:));
//    swizzleMethod([UILabel class], @selector(setTextColor:), @selector(setLabelTextColor:));
}

-(void)setLabelTintColor:(UIColor *)tintColor{
    
    if(self.forcedTintColor){
        [self setLabelTintColor:self.forcedTintColor];
        return;
    }
    if(self.text.tintColor){
        
        [self setLabelTintColor:self.text.tintColor];
        self.forcedTintColor = self.text.tintColor;
        return;
    }
    [self setLabelTintColor:tintColor];
}



//-(void)setLabelTextColor:(UIColor *)textColor{
//    
//    if(self.forcedTintColor){
//        [self setLabelTextColor:self.forcedTintColor];
//        return;
//    }
//    if(self.text.tintColor){
//        
//        [self setLabelTextColor:self.text.tintColor];
//        self.forcedTintColor = self.text.tintColor;
//        return;
//    }
//    [self setLabelTextColor:textColor];
//}


#pragma mark 不同大小的字体
- (void)setFristText:(NSString *)fristText :(int )fristfont andSecond:(NSString *)secondText :(int )Secondfont;
{
    if (secondText==nil) {
        secondText=@"";
    }
    self.textColor = [UIColor lightGrayColor];
    NSString *text = [NSString stringWithFormat:@"%@ %@",fristText,secondText];
    self.text = text;
    NSMutableAttributedString *attributedString = [[NSMutableAttributedString alloc] initWithString:self.text];
    
    [attributedString addAttribute:NSForegroundColorAttributeName value:[UIColor add_colorWithRGBHexString:@"666666"] range:[self.text rangeOfString:fristText]];
    [attributedString addAttribute:NSFontAttributeName value:[UIFont systemFontOfSize:fristfont] range:[self.text rangeOfString:fristText]];
    
    [attributedString addAttribute:NSForegroundColorAttributeName value:[UIColor add_colorWithRGBHexString:@"333333"] range:[self.text rangeOfString:secondText]];
    [attributedString addAttribute:NSFontAttributeName value:[UIFont systemFontOfSize:Secondfont] range:[self.text rangeOfString:secondText]];
    
    
    self.attributedText = attributedString;
    
}





@end
